function show_album_previews() {
    var url = window.location.href;
    var selector;

    if (url.startsWith("http://movie.douban.com")) {
        console.log("show cover previews");

        selector = 'div.cover a';
    } else if (url.startsWith("http://www.douban.com/photos/album")) {
        console.log("show album previews");

        selector = 'a.photolst_photo';
    } else {
        console.log("unknown gallery page @ " + url);

        return;
    }

    var count=0, items = [];

    items.push('<div>');

    $(selector).each(function (idx, photo) {
        var url = $(photo).attr('href'),
            title = $(photo).attr('title'),
            img = $(photo).find('img').attr('src');

        items.push('<img src="' + img + '" title="' + title + '" page="' + url + '">');

        if (++count == 5) {
            count = 0;
            items.push('</div><div>');
        }
    });

    items.push('</div>');

    console.log("found " + items.length + " photos");

    $('body').append(
    '<style type="text/css">' +
        'div.apple_overlay.black { background:url(' + chrome.extension.getURL('assets/img/overlay/transparent.png') + ') no-repeat; -webkit-background-size: cover;}' +
        '.scrollable { background:url(' + chrome.extension.getURL('assets/img/h300.png')+ ') repeat-x; }' +
        '.apple_overlay { background-image:url(' + chrome.extension.getURL('assets/img/overlay/white.png') + '); }' +
        '.apple_overlay .close { background-image:url(' + chrome.extension.getURL('assets/img/overlay/close.png') + '); }' +
    '</style>' +
	'<!--[if lt IE 7]>' +
	'<style>' +
        'div.apple_overlay { background-image:url(' + chrome.extension.getURL('assets/img/overlay/overlay_IE6.gif') + '); color:#fff; }' +
        'div.apple_overlay div.close { background-image:url(' + chrome.extension.getURL('assets/img/overlay/overlay_close_IE6.gif') + '); }' +
	'</style>' +
	'<![endif]-->' +
    '<script type="text/javascript" src="' + chrome.extension.getURL('/assets/lib/jquery/jquery.tools.js') + '"></script>' +
    '<div id="preview" class="apple_overlay black">' +
        '<div>' +
            '<div id="image_wrap">' +
                '<img src="' + chrome.extension.getURL('/assets/img/blank.gif') + '"/>' +
            '</div>' +
            '<div class="details"><p id="title"></p></div>'+
        '</div>' +
        '<div id="gallery">' +
            '<a class="prev browse left"></a>' +
            '<div class="scrollable">' +
                '<div class="items">' +
                    items.join('') +
                '</div>' +
            '</div>' +
            '<a class="next browse right"></a>' +
        '</div>' +
    '</div>');

    $(".scrollable").scrollable();

    $(".items img").click(function() {
        // see if same thumb is being clicked
        if ($(this).hasClass("active")) { return; }

        // calclulate large image's URL based on the thumbnail URL (flickr specific)
        var url = $(this).attr("src").replace("/thumb/", "/photo/");
        var title = $(this).attr("title");

        // get handle to element that wraps the image and make it semi-transparent
        var wrap = $("#image_wrap").fadeTo("medium", 0.5);

        var img = new Image();

        // call this function after it's loaded
        img.onload = function() {

            // make wrapper fully visible
            wrap.fadeTo("fast", 1);

            // change the image
            wrap.find("img").attr("src", url) .height(Math.min(this.height, 480));
            wrap.find("#title").text(title);
        };

        // begin loading the image from www.flickr.com
        img.src = url;

        // activate item
        $(".items img").removeClass("active");
        $(this).addClass("active");

    // when page loads simulate a "click" on the first image
    }).filter(":first").click();

    $('#preview').overlay({
        top: 10,
        load: true,
        effect: 'apple',
        mask: {
            // you might also consider a "transparent" color for the mask
            color: '#fff',
            // load mask a little faster
            loadSpeed: 200,
            // very transparent
            opacity: 0.5
        }
    });
}

read_option('douban_album_previews', function (k, v) {
    if (v) {
        show_album_previews();
    }
});
